package com.xyzwps.paimon.furina.modules.userinfo.entity

import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.upsert
import org.springframework.stereotype.Repository

@Repository
class UserInfoRepository {

    private val t = UserInfo.Companion.TABLE

    fun upsert(userInfo: UserInfo) {
        t.upsert {
            it[userId] = userInfo.userId
            it[nickname] = userInfo.nickname.value
            it[avatar] = userInfo.avatar.value
            it[description] = userInfo.description.value
            it[createTime] = userInfo.createTime
            it[updateTime] = userInfo.updateTime
        }
    }

    fun findByUserId(userId: Long): UserInfo? = t
        .select { t.userId eq userId }
        .map { UserInfo(it) }
        .firstOrNull()

    fun findByUserIds(userIds: Collection<Long>): List<UserInfo> {
        if (userIds.isEmpty()) return emptyList()

        return t
            .select { t.userId inList userIds }
            .map { UserInfo(it) }
    }
}